/**
 *@description 用户画像》产品需求偏好
 *@author yhw
 */
import {
  classQuery,
  effectQuery,
  productQuery,
  skuQuery,
  colorQuery
} from '../services/ProductNeed';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'ProductNeed',

  state: {
    classData: null,
    classLoading: false,
    effectData: null,
    effectLoading: false,
    productData: null,
    productLoading: false,
    skuData: null,
    skuLoading: false,
    colorData: null,
    colorLoading: false,
  },

  subscriptions: {},

  effects: { * classQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'classLoading'
      });

      const {
        data
      } = yield call(classQuery, parse(payload));

      if (data) {

        let temp = {
            legend: [],
            inside: [],
            outside: []
          },
          outIndex = 0,
          colors1 = ['#CFBB63', '#FF5239', '#858176'],
          colors2 = ['#CFBB63', '#C5B46D', '#D2C386', '#E6DCAC', '#FAF4D5', '#FF5239', '#F57A64', '#FF9B88', '#858176', '#918B7C', '#A39985', '#BCB094', '#E9DDB8'];
        data.data && data.data.map(function(item, index) {
          temp.inside.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors1[index]
              }
            }
          });
          item.detail.map(function(item2, index2) {
            temp.outside.push({
              name: item2.name,
              value: item2.value,
              itemStyle: {
                normal: {
                  color: colors2[outIndex]
                }
              }
            });
            outIndex++;
          });
        });

        yield put({
          type: 'classQuerySuccess',
          payload: {
            classData: temp,
          },
        });
      } else {
        error("获取品类倾向失败");
      }
    },
    //===================================
    * effectQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'effectLoading'
      });

      const {
        data
      } = yield call(effectQuery, parse(payload));

      if (data) {

        let max = 100,
          temp = {
            indicator: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7'];
        data.data && data.data.map(function(item, index) {
          temp.value.push(item.value);
          max = Math.max(item.value, max);
        });

        data.data && data.data.map(function(item, index) {
          temp.indicator.push({
            text: item.name + "：\r\n" + parseInt(item.value).formatMoney() + "",
            max: max
          });
        });

        yield put({
          type: 'effectQuerySuccess',
          payload: {
            effectData: temp,
          },
        });
      } else {
        error("获取功效倾向失败");
      }
    },
    //===================================
    * productQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'productLoading'
      });

      const {
        data
      } = yield call(productQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            classId: item.classId,
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'productQuerySuccess',
          payload: {
            productData: temp,
          },
        });
      } else {
        error("获取产品倾向失败");
      }
    },
    //===================================
    * skuQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'skuLoading'
      });

      const {
        data
      } = yield call(skuQuery, parse(payload));

      if (data) {
        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'skuQuerySuccess',
          payload: {
            skuData: temp,
          },
        });
      } else {
        error("获取用户SKU倾向数量失败");
      }
    },
    //===================================
    * colorQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'colorLoading'
      });

      const {
        data
      } = yield call(colorQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'colorQuerySuccess',
          payload: {
            colorData: temp,
          },
        });
      } else {
        error("获取颜色倾向失败");
      }
    },

  },

  reducers: {
    classLoading(state) {
      return {...state,
        classLoading: true
      };
    },
    classQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        classLoading: false
      };
    },
    //===================================
    effectLoading(state) {
      return {...state,
        effectLoading: true
      };
    },
    effectQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        effectLoading: false
      };
    },
    //===================================
    productLoading(state) {
      return {...state,
        productLoading: true
      };
    },
    productQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        productLoading: false
      };
    },
    //===================================
    skuLoading(state) {
      return {...state,
        skuLoading: true
      };
    },
    skuQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        skuLoading: false
      };
    },
    //===================================
    colorLoading(state) {
      return {...state,
        colorLoading: true
      };
    },
    colorQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        colorLoading: false
      };
    },

  },
};